package com.kws.merchant.util;

import com.kws.merchant.domain.CurrentLastTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间操作工具
 * @author :yxy
 * @date : 2024/10/28
 */
public class TimeUtil {

    public static final SimpleDateFormat DATETIME_FORMAT =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DATE_FORMAT =new SimpleDateFormat("yyyy-MM-dd");

    public static String getTime(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return DATETIME_FORMAT.format(calendar.getTime());
    }

    public static Date addDayByDate(int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }

    public static String addDayByStr(int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return DATETIME_FORMAT.format(calendar.getTime());
    }

    /**
     * 获得当前月
     * @return
     */
    public static int getMonth(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 月份加减
     * @param num
     * @return
     */
    public static int addMonth(int num){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, num);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取当前月份年份，上x月份年份
     * @param num
     * @return
     */
    public static CurrentLastTime currentLastTime(int num){
        Calendar calendar = Calendar.getInstance();

        // 获取当前月份
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1; // 因为从0开始计数
        int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
        //获取当月天数
        int currentMaxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        //正数+1 负数-1
        calendar.add(Calendar.MONTH, num);
        int lastYear = calendar.get(Calendar.YEAR);
        int lastMonth = calendar.get(Calendar.MONTH) + 1; // 同样因为从0开始计数
        int lastDay = calendar.get(Calendar.DAY_OF_MONTH);
        CurrentLastTime currentLastTime = new CurrentLastTime();
        currentLastTime.setCurrentMonth(currentMonth);
        currentLastTime.setCurrentYear(currentYear);
        currentLastTime.setLastMonth(lastMonth);
        currentLastTime.setLastYear(lastYear);
        currentLastTime.setCurrentDay(currentDay);
        currentLastTime.setLastDay(lastDay);
        currentLastTime.setCurrentMaxDays(currentMaxDays);
        return currentLastTime;
    }

    /**
     * 获取当前月份年份，上x月份年份
     * @return
     */
    public static CurrentLastTime currentDate(String time){
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(DATE_FORMAT.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 获取当前月份
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1; // 因为从0开始计数
        int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
        //获取当月天数
        int currentMaxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        CurrentLastTime currentLastTime = new CurrentLastTime();
        currentLastTime.setCurrentMonth(currentMonth);
        currentLastTime.setCurrentYear(currentYear);
        currentLastTime.setCurrentDay(currentDay);
        currentLastTime.setCurrentMaxDays(currentMaxDays);
        return currentLastTime;
    }

    public static int getDays(Date startTime,Date endTime){
        return Math.toIntExact((endTime.getTime() - startTime.getTime()) / 1000 / 60 / 60 / 24);
    }

}
